<?php
class model_areaprice extends framework_base_model {
    /**
     * 获取大分类列表
     * @param number $nextNum
     * @param number $pagesize
     * @param unknown $condition
     * @return unknown|Ambigous <number, unknown>
     */
    public static function get_areaprice_list($nextNum = 0,$pagesize = 0,$condition = array()) {
		$filed = "a.`produce_name`,a.`product_id`,a.`parent_id`,a.`area_motor_id`,a.`area_smotor_id`,a.`area_ship_id`,a.`area_train_id`,a.`raw_add_time`,a.`raw_update_time`,b.`product_class_id`,b.`class_name`,a.`status`,a.`author`,a.`editer`,c.`area_price_id`";
    	$sql = "select {$filed} from weixin_product a left join weixin_product_class b on a.`parent_id`=b.`product_class_id` left join `weixin_area_price` as c on a.`product_id`=c.`produce_id` and c.`delete_flag` = 0 where a.`delete_flag` = 0 ";
		$where = "";
    	if(is_array($condition)&& count($condition)>0){
    		if(isset($condition['class_big_name'])) {
    			$where .= " and b.`class_name` like '%{$condition['class_big_name']}%'";
    		}
			if(isset($condition['class_small_name'])) {
    			$where .= " and a.`produce_name` like '%{$condition['class_small_name']}%'";
    		}
    	}
    	//$where .= " order by a.`product_id` desc";
		$group = " group by a.`produce_name` order by a.`product_id` desc";
    	$limit = "";
        if($pagesize != 0 ){
    		$limit = " limit {$nextNum},{$pagesize}";
    	}
    	$result = array();
    	try {
    		$st = self::DB()->dbh->prepare($sql.$where.$group.$limit);
    		$st->execute();
    		$result = $st->fetchAll();
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		$result = array();
    	}
		
		$sql = "select count(a.`product_id`) from weixin_product a left join weixin_product_class b on a.`parent_id`=b.`product_class_id` where a.`delete_flag` = 0 {$where}";
		$binds2 = array();
    	try {
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute($binds2);
    		$count = $st->fetchColumn();
    		$result['count'] = $count;
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		$result['count'] = 0;
    	}		
		return $result;
    }
	//添加基础价
	public static function areaprice_add(array $data) {
    	if(!is_array($data)) {
    		return 0;
    	}
    	try{
    		$sql = "insert into `weixin_area_price` set ";
    		$binds = array();
    		foreach ($data as $k => $v) {
    			$sql .= '`' . $k . '`=:' . $k . ',';
    			$binds[':' . $k] = $v;
    		}
    		$sql .= 'raw_add_time=NOW()';
    		$arr=self::DB() -> dbh -> prepare($sql);
    		$rel = $arr->execute($binds);
    		if($rel == TRUE) {
    			return self::DB()->dbh->lastInsertId();
    		} else {
    			return 0;
    		}
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return 0;
    	}
    }
	//更新基础价
	public static function update_areaprice_info($id,$data) {
    	try{
    		$sql = 'UPDATE `weixin_area_price` SET ';
    		$binds = array();
    		foreach ($data as $k => $v) {
    			$sql .= '`' . $k . '`=:' . $k . ',';
    			$binds[':' . $k] = $v;
    		}
    		$ids = implode(',',$id);
    		$sql = trim($sql,',');
    		$sql .= ' WHERE `area_price_id` in ('.$ids.')';
    		$stm=self::DB() -> dbh -> prepare($sql);
    		$rel = $stm->execute($binds);
    		$rowCount = $stm -> rowCount();
    		return $rowCount;
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return -1;
    	}
    }
	public static function get_areaprice_by_priduceid($produce_id){
		$sql = "select `area_price_id`,`produce_id`,`area_name`,`pre_price`,`cur_price`,`sort`,`raw_add_time` from `weixin_area_price` where `delete_flag` = 0 and `produce_id`=:produce_id";
    	$result = array();
		try {
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute(array(':produce_id'=>$produce_id));
    		$result = $st->fetchAll();
			if(!empty($result)){
				foreach($result as $key=>$value){
					$result[$key]['rose'] = round($value['cur_price'] - $value['pre_price'],2);
				}
			}
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    	}
		return $result;
	}
	//根据id获取区域基础价数据
	public static function get_areaprice_by_id($price_id){
		$sql = "select `area_price_id`,`produce_id`,`area_name`,`pre_price`,`cur_price`,`sort`,`raw_add_time`,`author`,`editer` from `weixin_area_price` where `delete_flag` = 0 and `area_price_id`=:area_price_id";
    	$result = array();
		try {
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute(array(':area_price_id'=>$price_id));
    		$result = $st->fetch();
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    	}
		return $result;
	}
	//是否存在相同的区域基础价
	public static function areaprice_exists($area_name, $produce_id, $id=0){
		$info = array();

    	$sql = "select `area_price_id` from `weixin_area_price` where `area_name` = :area_name and `produce_id`=:produce_id and `delete_flag` = 0 ";
		$binds[":area_name"] = $area_name;
		$binds[":produce_id"] = $produce_id;
		if(!empty($id)){
			$sql .= " and `area_price_id` != :area_price_id";
			$binds[":area_price_id"] = $id;
		}
    	try {
    		$st = self::DB() -> dbh -> prepare($sql);
    		$st -> execute($binds);
    		$info = $st -> fetch();
    	} catch (Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    	}
    	return $info;
	}
}
